package com.example.mypc.three_linkage;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by MyPC on 2016/10/10.
 */
public class NodeTreeView extends ScrollView {

    private Context mContext;
    private List<Node> mDatas;
    private LinearLayout.LayoutParams innerLayoutParams;
    //分割线;
    private LinearLayout.LayoutParams dividerParams;
    //padding值;
    public int UNIT = 40;
    public NodeTreeView(Context context) {
        super(context);
        this.mContext = context;
        innerLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        dividerParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,2);
    }

    public NodeTreeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        innerLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        dividerParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,2);
    }

    //设置数据
    public void setData(List<Node> mDatas){
        this.mDatas = mDatas;
        //更新界面
        removeAllViews();
        //给界面添加数据
        initViews();
    }

    private void initViews(){
        LinearLayout linearLayout = new LinearLayout(mContext);
        ScrollView.LayoutParams rootLayoutParams =
                new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
        //设置LinearLayout方向
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        addView(linearLayout,rootLayoutParams);
        //添加一级菜单
       List<Node>  rootNodes = getListByPId(0);
        for (int i = 0 ;i<rootNodes.size();i++){
            Node nodes = rootNodes.get(i);
            //添加一个textView
            final TextView firstLevelView = new TextView(mContext);
            firstLevelView.setLayoutParams(innerLayoutParams);
            firstLevelView.setText(nodes.getName());
            firstLevelView.setTextSize(18);
            //设置id;
            firstLevelView.setId(nodes.getId());
            firstLevelView.setTextColor(Color.RED);
            firstLevelView.setBackgroundColor(Color.BLACK);
            firstLevelView.setPadding(UNIT,UNIT,UNIT,UNIT);


            // 创建二级菜单容器
            LinearLayout secondLayoutContainer = new LinearLayout(mContext);
            secondLayoutContainer.setLayoutParams(innerLayoutParams);
            secondLayoutContainer.setOrientation(LinearLayout.VERTICAL);
            secondLayoutContainer.setVisibility(GONE);

            firstLevelView.setTag(secondLayoutContainer); // 绑定一级菜单以及对应容器
            linearLayout.addView(firstLevelView); // 添加一级菜单
            linearLayout.addView(secondLayoutContainer); // 二级菜单容器
            linearLayout.addView(getDivider()); // 区分线

            //添加点击事件
            firstLevelView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    //当点击的时候隐藏以前点击的;
                    closeSameRank(firstLevelView);
                    //告诉这是谁的孩子
                    int myId = v.getId();
                    // 判断之前有没有添加过视图？
                    LinearLayout lnycontainer  = (LinearLayout) v.getTag();
                    if (lnycontainer.getChildCount()==0){
                        addingMenuChild(lnycontainer, myId);
                    }else if (lnycontainer.isShown()){
                        lnycontainer.setVisibility(GONE);
                    }else{
                        lnycontainer.setVisibility(VISIBLE);
                    }
                }
            });
        }
    }

    /**
     * 添加子菜单;
     * @param container
     * @param myId
     */
    private void addingMenuChild(LinearLayout container, int myId){
        container.setVisibility(VISIBLE);
        List<Node> childNodes = getListByPId(myId);
        if (childNodes.size()!=0){
            for (int i = 0 ;i<childNodes.size();i++){
                Node node = childNodes.get(i);
                final TextView childLevelView = new TextView(mContext);
                childLevelView.setLayoutParams(innerLayoutParams); //
                childLevelView.setText(node.getName());
                childLevelView.setTextSize(16);
                childLevelView.setId(node.getId());
                childLevelView.setTextColor(Color.BLACK);
                childLevelView.setBackgroundColor(Color.LTGRAY);
                childLevelView.setPadding(UNIT+(myId*10),UNIT,UNIT,UNIT);
                //创建容器
                LinearLayout childChildLayoutContainer = new LinearLayout(mContext);
                childChildLayoutContainer.setLayoutParams(innerLayoutParams);
                childChildLayoutContainer.setOrientation(LinearLayout.VERTICAL);
                childChildLayoutContainer.setVisibility(GONE);
                //设置标签;
                childLevelView.setTag(childChildLayoutContainer);
                container.addView(childLevelView);
                container.addView(childChildLayoutContainer);
                container.addView(getDivider());
                //当点击二级菜单的时候
                childLevelView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        closeSameRank(childLevelView);
                        int myId = v.getId();
                        // 判断之前有没有添加过视图？
                        LinearLayout lnycontainer  = (LinearLayout) v.getTag();
                        if (lnycontainer.getChildCount()==0){
                            addingMenuChild(lnycontainer, myId);
                        }else if (lnycontainer.isShown()){
                            lnycontainer.setVisibility(GONE);
                        }else{
                            lnycontainer.setVisibility(VISIBLE);
                        }
                    }
                });
            }

        }else {
            Toast.makeText(mContext, "没有孩子了！别点了！", Toast.LENGTH_SHORT).show();
        }
    }
    /**
     *
     * @param i 那个级别的;
     * @return
     */
    private List<Node> getListByPId(int i){
        List<Node> list = new ArrayList<>();
        for (int ii = 0 ;ii<mDatas.size();ii++){
            Node node = mDatas.get(ii);
            if (node.getpId()==i){
                list.add(node);
            }
        }
        return list;
    }

    /**
     * 获取分割线;
     */
    private View getDivider() {
        View view = new View(mContext);
        view.setLayoutParams(dividerParams);
        view.setBackgroundColor(Color.YELLOW);
        return view;
    }

    //
    private void closeSameRank(TextView childLevelView){
        if (childLevelView.getParent() instanceof  LinearLayout){
            LinearLayout lnyContainer = (LinearLayout) childLevelView.getParent();
            //遍历容器获得子view
            for(int i = 0 ;i<lnyContainer.getChildCount();i++){
                if (lnyContainer.getChildAt(i) instanceof TextView){
                    TextView tvitem = (TextView) lnyContainer.getChildAt(i);
                    if (tvitem!=null){
                        //textView 所在的父容器中;二级菜单
                       LinearLayout lny = (LinearLayout) tvitem.getTag();
                        if (!childLevelView.equals(tvitem)){
                            lny.setVisibility(GONE);
                        }
                    }
                }
            }
        }else{

        }
    }
}
